﻿// Developer Express Code Central Example:
// World Wide CRM Demo
// 
// The World Wide CRM demo is a next generation application with outstanding
// navigation capabilities and shows a reimagined view of customer contact and
// management. This demo showcases many of the most popular features of the
// WinForms Subscription.
// 
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E3993

using System;
using System.Diagnostics;
using System.Linq;
using DevExpress.CrmDemo.Win.Data;
using DevExpress.CrmDemo.Win.Controls;
using System.Windows.Forms;
using DevExpress.CrmDemo.Win.Helpers;

namespace DevExpress.CrmDemo.Win {
    public enum States {
        None,
        StartPage,
        Dashboard,
        OppDetail,
        ReportViewer,
        Scheduler
    }

    public partial class MainForm : DevExpress.XtraEditors.XtraForm {
        public MainForm() {
            InitializeComponent();
            ColorHelper.MainForm = this;
        }

        private void MainForm_Load(object sender, EventArgs e) {
            //modified by daniel
            //this.Size = DevExpress.Utils.Text.TextUtils.GetScaleSize(new System.Drawing.Size(1378, 768));
            this.Size = new System.Drawing.Size(1378, 768);
            var preloadData = DataProvider.Instance;

            var preloadDashboard = MyDashboard;

            State = States.StartPage;
        }

        private Dashboard myDashboard;

        public Dashboard MyDashboard {
            get {
                if (myDashboard == null) {
                    myDashboard = new Dashboard();
                    myDashboard.Dock = System.Windows.Forms.DockStyle.Fill;
                    myDashboard.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                    myDashboard.Opportunities = DataProvider.Instance.Opportunities;
                }

                return myDashboard;
            }
        }

        private OppDetail myDetail;

        public OppDetail MyDetail {
            get {
                if (myDetail == null) {
                    myDetail = new OppDetail();
                    myDetail.Dock = System.Windows.Forms.DockStyle.Fill;
                    myDetail.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myDetail;
            }
        }

        private Start myStart;

        public Start MyStart {
            get {
                if (myStart == null) {
                    myStart = new Start();
                    myStart.Dock = System.Windows.Forms.DockStyle.Fill;
                    myStart.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myStart;
            }
        }

        private ReportPage myReportPage;

        public ReportPage MyReportPage {
            get {
                if (myReportPage == null) {
                    myReportPage = new ReportPage();
                    myReportPage.Dock = System.Windows.Forms.DockStyle.Fill;
                    myReportPage.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myReportPage;
            }
        }

        private SchedulerPage mySchedulerPage;

        public SchedulerPage MySchedulerPage {
            get {
                if (mySchedulerPage == null) {
                    mySchedulerPage = new SchedulerPage(DataProvider.Instance.Appointments, DataProvider.Instance.TeamMembers);
                    mySchedulerPage.Dock = System.Windows.Forms.DockStyle.Fill;
                    mySchedulerPage.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return mySchedulerPage;
            }
        }

        void NavRequestHandler(object sender, NavRequestEventArgs e) {
            OpportunityId = e.OpportunityId;
            State = e.TargetState;
        }

        private States state;

        private States State {
            get { return state; }
            set {
                if (state != value) {
                    state = value;
                    LoadPage(state);
                }
            }
        }

        private int OpportunityId { get; set; }

        void LoadPage(States state) {
            Control newPage = null;

            switch (state) {
                case States.StartPage:
                    newPage = MyStart;
                    break;
                case States.Dashboard:
                    newPage = MyDashboard;
                    break;
                case States.OppDetail:
                    MyDetail.Opportunity = DataProvider.Instance.Opportunities.SingleOrDefault(o => o.ID == this.OpportunityId);
                    newPage = MyDetail;
                    break;
                case States.ReportViewer:
                    MyReportPage.Opportunities = DataProvider.Instance.Opportunities;
                    newPage = MyReportPage;
                    break;
                case States.Scheduler:
                    newPage = MySchedulerPage;
                    break;
                default:
                    break;
            }

            if (newPage != null) {
                this.Controls.Clear();
                this.Controls.Add(newPage);
            }
        }
    }

    public delegate void NavRequestEventHandler(object sender, NavRequestEventArgs e);

    public class NavRequestEventArgs : EventArgs {
        public NavRequestEventArgs(States targetState) {
            this.TargetState = targetState;
        }

        public NavRequestEventArgs(States targetState, int opportunityId) {
            this.TargetState = targetState;
            this.OpportunityId = opportunityId;
        }

        public States TargetState { get; set; }
        public int OpportunityId { get; set; }
    }
}